package com.longbo.demostudy.demo.smsVerification;

import org.apache.commons.codec.digest.DigestUtils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;

public class HttpUtil {
    public static String createCommonParam(){

        //时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String timeStamp = sdf.format(new Date());

        //签名
        String sig = DigestUtils.md5Hex(VerifyConfig.ACCCOUNT_SID+VerifyConfig.AUTH_TOKEN+timeStamp);
        return "&timestamp=" + timeStamp + "&sig=" + sig + "&respDataType=" + VerifyConfig.RESP_DATA_TYPE;

    }

    public static String post(String url,String body){
        System.out.println("url:" + System.lineSeparator() + url);
        System.out.println("body:" + System.lineSeparator() + body);
        String result = "";
        try{
            OutputStreamWriter out = null;
            BufferedReader in = null;
            URL realUrl = new URL(url);
            URLConnection conn = realUrl.openConnection();

            //设置连接参数
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(20000);
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

            //提交数据
            out = new OutputStreamWriter(conn.getOutputStream(),"UTF-8");
            out.write(body);
            out.flush();

            //读取返回数据
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
            String line = "";
            boolean firstLine = true;
            while (((line=in.readLine())!=null)){
                if (firstLine){
                    firstLine = false;
                }else {
                    result += line;
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    /**
     * 回调测试工具方法
     *
     * @param url
     * @return
     */
    public static String postHuiDiao(String url, String body)
    {
        String result = "";
        try
        {
            OutputStreamWriter out = null;
            BufferedReader in = null;
            URL realUrl = new URL(url);
            URLConnection conn = realUrl.openConnection();

            // 设置连接参数
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(20000);

            // 提交数据
            out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
            out.write(body);
            out.flush();

            // 读取返回数据
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            String line = "";
            boolean firstLine = true; // 读第一行不加换行符
            while ((line = in.readLine()) != null)
            {
                if (firstLine)
                {
                    firstLine = false;
                } else
                {
                    result += System.lineSeparator();
                }
                result += line;
            }
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return result;
    }
}
